package au.com.helloweb.androidgamepractice.planegame2;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Player {
	private int playerBlood = 3;
	private Bitmap playerBloodImg;

	// player's location and image
	public int x, y;
	private Bitmap playerImg;
	private int speed = 5;
	private boolean isCollision =false;
	private int noCollisionCount = 0;//collision number
	private int CollisionTime = 60;//is no die in collision time 

	public Player(Bitmap playerImg, Bitmap playerBloodImg) {
		this.playerImg = playerImg;
		this.playerBloodImg = playerBloodImg;
		x = MySurfaceView.screenW / 2 - playerImg.getWidth() / 2;
		y = MySurfaceView.screenH - playerImg.getHeight();
	}

	public void draw(Canvas canvas, Paint paint) {
		if(isCollision){
			//no die status, flash image
			if(noCollisionCount %2 == 0){
				canvas.drawBitmap(playerImg, x, y, paint);
			}
		}else{
			canvas.drawBitmap(playerImg, x, y, paint);
		}
		

		// draw blood image
		for (int i = 0; i < playerBlood; i++) {
			canvas.drawBitmap(playerBloodImg, i * playerBloodImg.getWidth(),
					MySurfaceView.screenH - playerBloodImg.getHeight(), paint);
		}
	}

	public void logic(int status) {
		switch (status) {
		case 0:// 水平
			break;
		case 1:// 左倾
			x=x-5;
			if(x <=0){
				x = 0;
			}
			break;
		case 2:// 右倾
			x=x+5;
			if(x >= MySurfaceView.screenW){
				x = MySurfaceView.screenW;
			}
			break;
		}
		if(isCollision){
			noCollisionCount++;
			if(noCollisionCount > CollisionTime){
				isCollision = false;
				noCollisionCount = 0;
			}
		}
	}

	public int getPlayerBlood() {
		return playerBlood;
	}

	public void setPlayerBlood(int playerBlood) {
		this.playerBlood = playerBlood;
	}

	//if cross with enemy, then die
	public boolean isCollsionWith(Enemy en){
		if(!isCollision){
			int x2 = en.x;
			int y2 = en.y;
			int w2 = en.frameW;
			int h2 = en.frameH;
			if(x > x2+w2){
				return false;
			}else if(x < x2){
				return false;
			}else if (y > y2+h2){
				return false;
			}else if( y< y2){
				return false;
			}
			isCollision = true;
			return true;
		}
		return false;
	}
	
	public boolean isCollsionWith(Bullet bt){
		if(!isCollision){
			int x2 = bt.bulletX;
			int y2 = bt.bulletY;
			int w2 = bt.bmpBullet.getWidth();
			int h2 = bt.bmpBullet.getHeight();
			if(x > x2+w2){
				return false;
			}else if(x < x2){
				return false;
			}else if (y > y2+h2){
				return false;
			}else if( y< y2){
				return false;
			}
			isCollision = true;
			return true;
		}
		return false;
	}
}
